package com.edu.zhku.wms.service;

import cn.hutool.core.util.StrUtil;
import com.edu.zhku.wms.mapper.EmployeeMapper;
import com.edu.zhku.wms.pojo.Employee;

import com.edu.zhku.wms.pojo.EmployeeExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    UserService userService;

    @Override
    public Employee selectByPrimaryKey(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Employee> list(String keyword, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        EmployeeExample employeeExample = new EmployeeExample();

        if (StrUtil.isNotBlank(keyword)) {
            keyword = "%" + keyword + "%";
            employeeExample.createCriteria().andNameLike(keyword);
            employeeExample.or(employeeExample.createCriteria().andEmployeeNoLike(keyword));
            employeeExample.or(employeeExample.createCriteria().andPositionLike(keyword));
        }

        return new PageInfo<>(employeeMapper.selectByExample(employeeExample));
    }

    @Override
    public int addEmploy(Employee employee) {

        // 工号employeeNo 和 身份证号码idCardNo 不能重复
        if (employee.getEmployeeNo() != null) {
            EmployeeExample employeeExample = new EmployeeExample();
            employeeExample.createCriteria().andEmployeeNoEqualTo(employee.getEmployeeNo());
            if (employeeMapper.countByExample(employeeExample) > 0) {
                return 0;
            }
        }
        if (employee.getIdCardNo() != null) {
            EmployeeExample employeeExample = new EmployeeExample();
            employeeExample.createCriteria().andIdCardNoEqualTo(employee.getIdCardNo());
            if (employeeMapper.countByExample(employeeExample) > 0) {
                return 0;
            }
        }

        // 一个员工要么不绑定账号，要么绑定一个未被绑定的且存在的账号
        if (employee.getUserId() != null && !userService.canBeBound(employee.getUserId())) {
            return 0;
        }

        employee.setId(null);

        return employeeMapper.insertSelective(employee);
    }


    @Override
    public int deleteEmployByPrimaryKey(Integer id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateEmploy(Employee employee) {

        // 要进行修改的员工在数据库中的原来的信息
        Employee employeeInDB = employeeMapper.selectByPrimaryKey(employee.getId());

        if (employeeInDB == null) {
            return 0;
        }

        // 如果要对工号employeeNo进行修改
        if (employee.getEmployeeNo() != null && !StrUtil.equals(employee.getEmployeeNo(), employeeInDB.getEmployeeNo())) {
            EmployeeExample employeeExample = new EmployeeExample();
            employeeExample.createCriteria().andEmployeeNoEqualTo(employee.getEmployeeNo());
            // 工号employeeNo不能重复
            if (employeeMapper.countByExample(employeeExample) > 0) {
                return 0;
            }
        }

        // 如果要对身份证号码idCardNo进行修改
        if (!StrUtil.equals(employee.getIdCardNo(), employeeInDB.getIdCardNo())) {
            EmployeeExample employeeExample = new EmployeeExample();
            employeeExample.createCriteria().andIdCardNoEqualTo(employee.getIdCardNo());
            // 身份证号码idCardNo不能重复
            if (employeeMapper.countByExample(employeeExample) > 0) {
                return 0;
            }
        }

        // 如果要改绑账号
        if (employee.getUserId() != null && !employee.getUserId().equals(employeeInDB.getUserId())) {
            if ( !userService.canBeBound(employee.getUserId())) {
                return 0;
            }
        }

        return employeeMapper.updateByPrimaryKey(employee);
    }
}
